package com.utooo.bid.service;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.request.ChatRequest;
import dev.langchain4j.model.chat.request.ChatRequestParameters;
import dev.langchain4j.model.chat.request.ResponseFormat;
import dev.langchain4j.model.chat.request.ResponseFormatType;
import dev.langchain4j.model.chat.request.json.JsonObjectSchema;
import dev.langchain4j.model.chat.request.json.JsonSchema;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.output.Response;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
@Slf4j
@RequiredArgsConstructor
public class LangChainService {


    private final ChatLanguageModel chatLanguageModel;
//    private final RedisChatMemoryStore chatMemoryStore;


    /*public String generate(List<ChatMessage> messages, String modelType) {
        try {
            // 调用模型
            Response<AiMessage> response = chatLanguageModel.generate(messages);
            return response.content().text();
        } catch (Exception e) {
            log.error("调用AI模型失败", e);
            throw new RuntimeException("AI分析失败: " + e.getMessage());
        }
    }*/

    public String startConversation() {
        return UUID.randomUUID().toString();
    }

    public String sendMessage(String conversationId, List<ChatMessage> messages) {
        ChatMemory chatMemory = MessageWindowChatMemory.builder()
                .id(conversationId)
                .maxMessages(10)
//                .chatMemoryStore(chatMemoryStore)
                .build();
        // 添加用户消息到对话历史
        messages.forEach(chatMemory::add);
        // 生成 AI 回复并添加到历史
        AiMessage aiMessage = chatLanguageModel.chat(chatMemory.messages()).aiMessage();
//        AiMessage aiMessage = AiMessage.from("");
        chatMemory.add(aiMessage);
        return aiMessage.text();
    }

    public String sendMessageFormat(String conversationId, List<ChatMessage> messages) {
        ChatMemory chatMemory = MessageWindowChatMemory.builder()
                .id(conversationId)
                .maxMessages(10)
//                .chatMemoryStore(chatMemoryStore)
                .build();
        messages.forEach(chatMemory::add);

        ResponseFormat responseFormat = ResponseFormat.builder()
                .type(ResponseFormatType.JSON)
                .jsonSchema(JsonSchema.builder()
                        .rootElement(JsonObjectSchema.builder()
                                .addIntegerProperty("catalogName", "节点名称")
                                .addIntegerProperty("sectionNumber", "层级序号")
                                .addIntegerProperty("children", "子节点集合")
                                .required("catalogName", "sectionNumber", "children")
                                .build())
                        .build())
                .build();

        ChatResponse chat = chatLanguageModel.chat(ChatRequest.builder()
                .messages(messages)
                .parameters(ChatRequestParameters
                        .builder()
                        .responseFormat(responseFormat)
                        .build())
                .build());
        AiMessage aiMessage = chat.aiMessage();
        chatMemory.add(aiMessage);
        return aiMessage.text();
    }

    public void endConversation(String conversationId) {
        ChatMemory chatMemory = MessageWindowChatMemory.builder()
                .id(conversationId)
                .maxMessages(10)
//                .chatMemoryStore(chatMemoryStore)
                .build();
        chatMemory.clear();
    }

}